import { NativeHelper } from "../../native/NativeHelper";
import { IPublisher } from "../IPublisher";

/*
@name: NativePublisher
@desc: 原生发行商SDK适配
@author: timoo
@date: 2022/11/28
*/
export class NativePublisher extends IPublisher{
    //平台ID
    protected _className:string = "sdk.{0}.SDKInterfaces";

    init(type:string):IPublisher{
        this._className = this._className.replace("{0}", type);
        return super.init(type);
    }
    /**
     * 初始化平台SDK
     * @param caller 回调this
     * @param onSuccess 成功回调
     * @param onFail 失败回调
     * @returns 
     */
    initSDK(caller?:any, onSuccess?:Function, onFail?:Function): void {
        let callbacks:string[] = [];
        if(caller && onSuccess != null){
            callbacks.push(NativeHelper.registerCallbackOnce(caller, onSuccess));
        }
        if(caller && onFail != null){
            callbacks.push(NativeHelper.registerCallbackOnce(caller, onFail));
        }
        NativeHelper.call(this._className, "initSDK", ...callbacks);
    }

    /**
     * 进入平台界面
     */
    showView(viewName:string, caller?: any, onClose?: Function): void {
        let callbacks:string[] = [viewName];
        if(caller && onClose != null){
            callbacks.push(NativeHelper.registerCallbackOnce(caller, onClose));
        }
        NativeHelper.call(this._className, "showView", callbacks);
    }
    

    /**
     * 推出APP
     */
    exit(): void {
        NativeHelper.call(this._className, "exit");
    }
}